package PaChong;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @description:
 * @author: 张德长 dechang.zhang@ucarinc.com
 * @date: 13:57:59 2018/10/31
 * @version:2.0
 */
public class Category {
    public static String getCPage = "http://x.yupoo.com/photos/18232209456/albums";
    public static String pageUrl = "http://x.yupoo.com";
    public String categoryName;
    public String categoryUrl;
    public List<Product> products;
    public boolean isSuccess = false;

    public static List<Category> getAllCategory() {
        List<Category> categories = new ArrayList();
        Elements selectedDoms = getCategoryDom();
        Iterator<Element> doms = selectedDoms.iterator();
        while (doms.hasNext()) {
            Elements selectedDomAs = doms.next().select("a");
            getCategoryA(selectedDomAs, categories);
        }
        return categories;
    }

    public static Elements getCategoryDom() {
        String html = "";
        try {
            html = Html.getHtml(getCPage);
        } catch (IOException e) {
            System.out.println("获取分类失败" + getCPage);
        }
        Document doc = Jsoup.parse(html);
        Elements selectedDoms = doc.select("ul.showheader__category");//"form.pagination__jumpwrap"
        return selectedDoms;
    }

    public static void getCategoryA(Elements selectedDomAs, List<Category> categories) {
        Iterator<Element> domAs = selectedDomAs.iterator();
        while (domAs.hasNext()) {
            Element selectedDomA = domAs.next();
            String tmpUrl = selectedDomA.attr("href");
            Elements selectedDomLis = selectedDomA.select("li.text_overflow");
            Iterator<Element> domLis = selectedDomLis.iterator();
            while (domLis.hasNext()) {
                Element selectedDomLi = domLis.next();
                Category category = new Category();
                category.categoryName = selectedDomLi.text();
                category.categoryUrl = Category.pageUrl + tmpUrl;

                categories.add(category);
            }
        }
    }

    public void initProducts() {
        products = new ArrayList();
        //需要一次网络链接  获取当前类别下所有产品插入链表
        Product product = new Product();
        products.add(product);
    }
}
